<?php
/**
* @version		1.0 : view.html.php marum56
* @package		Joomla
*/

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );

jimport( 'joomla.application.component.view');


/**
 * HTML View class for the Ojects component
 *
 * @static
 * @package	Joomla
 * @subpackage	Zamowienia
 * @since 1.0
 */
class ZamowieniaViewZamowienie extends JView
{	
	function display($tpl = null)
	{       
		$gid = JRequest::getVar( 'gid', '' );
		$edit = JRequest::getVar('edit', true);

		$db =& JFactory::getDBO();
		
		if ($edit)
			$ob =& msZamowienie::getInstance( $gid );
		else
			$ob =& msZamowienie::getInstance();

		// Binduj obiekt producenta, gdy wracamy po nieudanej
		// probie zapisu danych.
		// W normalnej sytuacji, gdy wyswietlamy szczegoly to
		// obiekt wypelniamy polami z tabeli.
		$post = JRequest::get('post');
		if ($post) {
			$ob->bind($post);
		}
			
		// Lista select przedmiotów zamówienia
		$lists['przedmiot_zam'] = msSelect::Czesci_zamienne('przedmiot_zam', $ob->przedmiot_zam);
		
		// Lista select jednostek miary
		$lists['jm'] = msSelect::Miara('jm', $ob->jm);
		/*
		$oP =& new msZamowienie( );
		$oP->setValue('obiekt','');
		$lists['obiekt'] = msSelect::Obiekt('obiekt', $oP->getValue('obiekt'), true, 'gid', 'txt');
	/*
	 * Lista selekt obiektów
	*/
	$query = 'SELECT obiekt AS value, obiekt AS text'
	. ' FROM #__obiekty AS o'
	. ' WHERE o.registerDate = (SELECT max(registerDate) 
		                          FROM #__obiekty
                                          WHERE gid = o.gid)'
        . ' AND o.del = 0'
        . ' ORDER BY text'
 	;
	@ $lists['obiekt'] =  msSelect::Obiekt( 'obiekt', 'Wybierz obiekt', $query, 'obiekt', true, 'obiekt', 'obiekt');
	
	/*
	 * Lista selekt spółek
	 */
	 function getSpolka( $gid )
	{	
	$query = 'SELECT f.spolka
		FROM #__obiekty AS o, #__obiekty_s AS s, #__spolka AS f
		WHERE s.gid = '.$gid
		.' AND s.spolka = f.id
		AND s.registerDate = (SELECT max(registerDate) 
		                          FROM #__obiekty_s
                                          WHERE gid = s.gid)'
        . ' ORDER BY text'
 	;
 	$this->db->setQuery( $query );
		$arr = $this->db->loadAssoc();
		
		if ( empty($arr )) {
			$arr = $this->getSpolka( $gid );
		}
		
		return $arr;	
	}
	
	//@ $lists['spolka'] =  msSelect::selekt( 'spolka', 'spolka', $query, '', true, 'spolka', 'spolka', 'adminFormSpolka' );
	
	/*
	 * Lista selekt kierownik obiektu
	 */
	$query = 'SELECT name AS value, name AS text'
	. ' FROM #__users AS u, #__obiekty_k AS k'
	. ' WHERE o.gid = k.gid'
	. ' AND k.users = u.pesel'
	. ' AND u.registerDate = (SELECT max(registerDate) 
		                          FROM #__users
                                          WHERE pesel = u.pesel)'
	. ' AND u.del = 0'
        . ' ORDER BY text'
 	;
	@ $lists['name'] =  msSelect::selekt( 'name', 'osoba kontaktowa', $query, 'name', true, 'name', 'name', 'adminFormName' );
	
	/*
	 * Lista selekt sprzętu - marka
	 */
	$query = 'SELECT mo1 AS value, mo1 AS text'
	. ' FROM #__maszyny AS m, #__maszyny_obiekt AS n'
	. ' WHERE o.gid = n.obiekt'
	. ' AND n.gid = m.gid'
 	. ' AND m.registerDate = (SELECT max(registerDate) 
		                          FROM #__maszyny
                                          WHERE gid = m.gid)'
	. ' AND m.del = 0'
        . ' ORDER BY text'
 	;
	@ $lists['marka'] =  msSelect::selekt( 'marka', 'marka', $query, 'marka', true, 'mo1', 'mo1', 'adminFormName' );
	
	/*
	 * Lista selekt sprzętu - model
	 */
	$query = 'SELECT mo2 AS value, mo2 AS text'
	. ' FROM #__maszyny AS m, #__maszyny_obiekt AS n'
	. ' WHERE o.gid = n.obiekt'
	. ' AND n.gid = m.gid'
 	. ' AND m.registerDate = (SELECT max(registerDate) 
		                          FROM #__maszyny
                                          WHERE gid = m.gid)'
	. ' AND m.del = 0'
        . ' ORDER BY text'
 	;
	@ $lists['model'] =  msSelect::selekt( 'model', 'model', $query, 'model', true, 'mo2', 'mo2', 'adminFormName' );
	
	/*
	 * Lista selekt sprzętu - nri
	 */
	$query = 'SELECT nri AS value, nri AS text'
	. ' FROM #__maszyny AS m, #__maszyny_obiekt AS n'
	. ' WHERE o.gid = n.obiekt'
	. ' AND n.gid = m.gid'
 	. ' AND m.registerDate = (SELECT max(registerDate) 
		                          FROM #__maszyny
                                          WHERE gid = m.gid)'
	. ' AND m.del = 0'
        . ' ORDER BY text'
 	;
	@ $lists['nri'] =  msSelect::selekt( 'nri', 'numer inwentarzowy', $query, 'nri', true, 'nri', 'nri' );
	
	/*
	 * Lista selekt sprzętu - nr fabryczny
	 */
	$query = 'SELECT mo4 AS value, mo4 AS text'
	. ' FROM #__maszyny AS m, #__maszyny_obiekt AS n'
	. ' WHERE o.gid = n.obiekt'
	. ' AND n.gid = m.gid'
 	. ' AND m.registerDate = (SELECT max(registerDate) 
		                          FROM #__maszyny
                                          WHERE gid = m.gid)'
	. ' AND m.del = 0'
        . ' ORDER BY text'
 	;
 	@ $lists['nr_fabryczny'] =  msSelect::selekt( 'nr_fabryczny', 'nr fabryczny', $query, 'mo4', true, 'mo4', 'mo4' );
		
	$this->assignRef('lists', $lists);	
		$this->assignRef('ob', $ob);

		parent::display($tpl);
	}
}
